package com.ebiz.jo.imageBrowser.pop
{
	import com.greensock.TweenLite;
	import com.hurlant.crypto.symmetric.CBCMode;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class PopBrowser extends Sprite
	{
		[Embed(source="jopop.swf", symbol="CLoadingMovie")]
		private var CLoadingMovie:Class;
		
		[Embed(source="jopop.swf", symbol="LinkBrowser")]
		private var CBrowser:Class;
		
		private var loadingMovie:MovieClip;
		
		[Embed(source="jopop.swf", symbol="LinkItemRenderer")]
		private var CItemRenderer:Class;
		
		private var browser:Sprite;
		private var origiPoint0:Point = new Point();
		private var origiPoint1:Point = new Point();
		private var buttonClose:SimpleButton;
		private var buttonUp:SimpleButton;
		private var buttonDown:SimpleButton;
		
		private var items:Vector.<PopItemRenderer> = new Vector.<PopItemRenderer>;
		
		private var currentIndex:int=0;
		
		/**
		 *If button switch can use. When button is clicked and tweenLite is actived, this variable = false 
		 */		
		private var activeButtonSwitch:Boolean = true;
		
		public function PopBrowser()
		{
			super();
			this.visible = false;

			this.x = 540;
			this.y = 280;
			//
			
			this.loadingMovie = new CLoadingMovie() as MovieClip;
			this.addChild(this.loadingMovie);
			this.loadingMovie.visible = false;
			this.loadingMovie.x = 0;
			this.loadingMovie.y = 0;
			//drag background for mouse click
			this.graphics.beginFill(0x0000FF,00);
			this.graphics.drawRect(-540,-280,1080,560);
			this.graphics.endFill();
			//add new browser for link
			this.browser = new CBrowser() as Sprite;
			this.addChild(this.browser);
			this.distill();
		}
		private function distill():void{
			buttonUp = this.browser.getChildByName("linkButtonUp") as SimpleButton;
			buttonDown = this.browser.getChildByName("linkButtonDown") as SimpleButton;
			buttonClose = this.browser.getChildByName("linkButtonClose") as SimpleButton;
			this.buttonUp.addEventListener(MouseEvent.MOUSE_DOWN,buttonUp_mouseDownHandler);
			this.buttonDown.addEventListener(MouseEvent.MOUSE_DOWN,buttonDown_mouseDownHandler);
			this.buttonClose.addEventListener(MouseEvent.MOUSE_DOWN,buttonClose_mouseDownHandler);
			//
			var $origi0:Sprite = this.browser.getChildByName("linkItem0") as Sprite;
			var $origi1:Sprite = this.browser.getChildByName("linkItem1") as Sprite;
			origiPoint0.x = $origi0.x;
			origiPoint0.y = $origi0.y;
			origiPoint1.x = $origi1.x;
			origiPoint1.y = $origi1.y;
			this.browser.removeChild($origi0);
			this.browser.removeChild($origi1);
		}
		public function clear():void{
			if(this.visible=true){
				this.visible=false;
			}
			for(var i:int=0;i<items.length;i++){
				if(this.items[i].view.parent){
					this.browser.removeChild(this.items[i].view);
				}
			}
			this.items = new Vector.<PopItemRenderer>;
		}
		
		public function show($id:int):void{
			this.clear();
			this.visible = true;
			//showloading
			this.loadingMovie.visible = true;
			//load data from gateway
			var $url:String = "gatewayjopop.php?id="+$id;
			var urlLoader:URLLoader = new URLLoader();
			urlLoader.addEventListener(Event.COMPLETE,urlLoader_completeHandler);
			urlLoader.load(new URLRequest($url));
		}
		
		private function urlLoader_completeHandler(event:Event):void{
			(event.currentTarget as URLLoader).removeEventListener(Event.COMPLETE,urlLoader_completeHandler);
			var $resultContent:String = String((event.currentTarget as URLLoader).data);
			$resultContent = $resultContent.split("<result_content>")[1].toString().split("</result_content>")[0];	
			var $resultXML:XML = XML($resultContent);
			var $itemXMLList:XMLList = $resultXML.item;
			var $vec:Vector.<PopItem> = this.tranXMLListToArray($itemXMLList);
			//
			createItems($vec);
			addItems();
		}
		
		private function tranXMLListToArray($xmlList:XMLList):Vector.<PopItem>{
			var $arr:Vector.<PopItem> = new Vector.<PopItem>($xmlList.length());
			for(var i:int=0;i<$xmlList.length();i++){
				var $xml:XML = $xmlList[i];
				var $item:PopItem = new PopItem();
				$item.name = $xml.@name;
				$item.description = $xml.@description;
				$item.image = $xml.@image;
				$item.link = $xml.@link;
				$arr[i] = $item;
			}
			return $arr;
		}
		private function createItems($vec:Vector.<PopItem>):void{
			for(var i:int=0;i<$vec.length;i++){
				this.items[i] = new PopItemRenderer(new CItemRenderer() as Sprite,$vec[i]);
				this.browser.addChild(this.items[i].view);
				this.items[i].view.visible = false;
			}
			//
			this.addChild(this.buttonClose);
			this.addChild(this.buttonDown);
			this.addChild(this.buttonUp);
		}
		
		private function addItems():void{
			
			(this.items[0].view).x = origiPoint0.x;
			(this.items[0].view).y = origiPoint0.y;
			(this.items[1].view).x = origiPoint1.x;
			(this.items[1].view).y = origiPoint1.y;
			this.items[0].view.visible = true;
			this.items[1].view.visible = true;
			
			this.currentIndex = 0;
		}
		
		private function buttonUp_mouseDownHandler(event:MouseEvent):void{
			if(!activeButtonSwitch){return;}
			
			var $lastIndex:int = this.currentIndex;
			this.currentIndex = this.rangeInt(this.currentIndex-1);
			
			activeButtonSwitch = false;
			
			this.items[rangeInt(currentIndex)].view.x = origiPoint0.x;
			this.items[rangeInt(currentIndex)].view.y = origiPoint0.y-40;
			this.items[rangeInt(currentIndex)].view.alpha = 0;
			this.items[rangeInt(currentIndex)].view.visible = true;
			
			this.items[rangeInt($lastIndex)].view.x = origiPoint0.x;
			this.items[rangeInt($lastIndex)].view.y = origiPoint0.y;
			this.items[rangeInt($lastIndex)].view.alpha = 1;
			this.items[rangeInt($lastIndex)].view.visible = true;
			
			
			TweenLite.to(this.items[rangeInt(this.currentIndex)].view,0.4,{delay:0.3,alpha:1,y:origiPoint0.y,x:origiPoint0.x,onComplete:tweenCompleteHandler});//focus in 0
			TweenLite.to(this.items[rangeInt($lastIndex)].view,0.4,{delay:0.15,alpha:1,y:origiPoint1.y,x:origiPoint1.x});//down to 1
			TweenLite.to(this.items[rangeInt($lastIndex+1)].view,0.4,{alpha:0,y:origiPoint1.y+40,x:origiPoint1.x});//focus out
		}
		
		private function buttonDown_mouseDownHandler(event:MouseEvent):void{
			if(!activeButtonSwitch){return;}
			
			var $lastIndex:int = this.currentIndex;
			this.currentIndex = this.rangeInt(this.currentIndex+1);
			
			activeButtonSwitch = false;
			
			this.items[rangeInt(currentIndex+1)].view.x = origiPoint1.x;
			this.items[rangeInt(currentIndex+1)].view.y = origiPoint1.y+40;
			this.items[rangeInt(currentIndex+1)].view.alpha = 0;
			this.items[rangeInt(currentIndex+1)].view.visible = true;
			
			this.items[rangeInt(currentIndex)].view.x = origiPoint1.x;
			this.items[rangeInt(currentIndex)].view.y = origiPoint1.y;
			this.items[rangeInt(currentIndex)].view.alpha = 1;
			this.items[rangeInt(currentIndex)].view.visible = true;
			
			TweenLite.to(this.items[rangeInt($lastIndex)].view,0.4,{alpha:0,y:origiPoint0.y-40,x:origiPoint0.x});//up to focus out
			TweenLite.to(this.items[rangeInt(this.currentIndex)].view,0.4,{delay:0.15,alpha:1,y:origiPoint0.y,x:origiPoint0.x});//up to 0
			TweenLite.to(this.items[rangeInt(this.currentIndex+1)].view,0.4,{delay:0.3,alpha:1,y:origiPoint1.y,x:origiPoint1.x,onComplete:tweenCompleteHandler});//up to focus in 1
		}
		
		private function rangeInt(value:int):int{
			if(value<0){
				return this.items.length-1;
			}
			if(value>=this.items.length){
				return 0;
			}
			return value;
		}
		
		private function tweenCompleteHandler():void{
			activeButtonSwitch = true;
		}
		/**
		 * close button 
		 * @param event
		 * 
		 */		
		private function buttonClose_mouseDownHandler(event:MouseEvent):void{
			this.clear();
		}
		
	}
}